home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Atari Mega Archive 1
/
Atari Mega Archive - Volume 1.iso
/
mint
/
editors
/
mntemacs.zoo
/
src
/
malloc.c
< prev
next >
Wrap
C/C++ Source or Header
|
1991-12-02
|
21KB
|
838 lines
/* dynamic memory allocation for GNU.
Copyright (C) 1985, 1987 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 1, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
In other words, you are welcome to use, share and improve this program.
You are forbidden to forbid anyone else to use, share and improve
what you give them. Help stamp out software-hoarding! */
/*
* @(#)nmalloc.c 1 (Caltech) 2/21/82
*
* U of M Modified: 20 Jun 1983 ACT: strange hacks for Emacs
*
* Nov 1983, Mike@BRL, Added support for 4.1C/4.2 BSD.
*
* This is a very fast storage allocator. It allocates blocks of a small
* number of different sizes, and keeps free lists of each size. Blocks
* that don't exactly fit are passed up to the next larger size. In this
* implementation, the available sizes are (2^n)-4 (or -16) bytes long.
* This is designed for use in a program that uses vast quantities of
* memory, but bombs when it runs out. To make it a little better, it
* warns the user when he starts to get near the end.
*
* June 84, ACT: modified rcheck code to check the range given to malloc,
* rather than the range determined by the 2-power used.
*
* Jan 85, RMS: calls malloc_warning to issue warning on nearly full.
* No longer Emacs-specific; can serve as all-purpose malloc for GNU.
* You should call malloc_init to reinitialize after loading dumped Emacs.
* Call malloc_stats to get info on memory stats if MSTATS turned on.
* realloc knows how to return same block given, just changing its size,
* if the power of 2 is correct.
*/
/*
* nextf[i] is the pointer to the next free block of size 2^(i+3). The
* smallest allocatable block is 8 bytes. The overhead information will
* go in the first int of the block, and the returned pointer will point
* to the second.
*
#ifdef MSTATS
* nmalloc[i] is the difference between the number of mallocs and frees
* for a given block size.
#endif MSTATS
*/
#ifdef emacs
/* config.h specifies which kind of system this is. */
#include "config.h"
#include <signal.h>
#else
/* Determine which kind of system this is. */
#include <signal.h>
#ifndef SIGTSTP
#ifndef VMS
#ifndef USG
#define USG
#endif
#endif /* not VMS */
#else /* SIGTSTP */
#ifdef SIGIO
#define BSD4_2
#endif /* SIGIO */
#endif /* SIGTSTP */
#endif /* not emacs */
/* Define getpagesize () if the system does not. */
#include "getpagesize.h"
#ifdef BSD
#ifdef BSD4_1
#include <sys/vlimit.h> /* warn the user when near the end */
#else /* if 4.2 or newer */
#include <sys/time.h>
#include <sys/resource.h>
#endif /* if 4.2 or newer */
#endif
#ifdef VMS
#include "vlimit.h"
#endif
extern char *start_of_data ();
#ifdef BSD
#ifndef DATA_SEG_BITS
#define start_of_data() &etext
#endif
#endif
#ifndef emacs
#define start_of_data() &etext
#endif
#define ISALLOC ((char) 0xf7) /* magic byte that implies allocation */
#define ISFREE ((char) 0x54) /* magic byte that implies free block */
/* this is for error checking only */
#define ISMEMALIGN ((char) 0xd6) /* Stored before the value returned by
memalign, with the rest of the word
being the distance to the true
beginning of the block. */
extern char etext;
/* These two are for user programs to look at, when they are interested. */
unsigned int malloc_sbrk_used; /* amount of data space used now */
unsigned int malloc_sbrk_unused; /* amount more we can have */
/* start of data space; can be changed by calling init_malloc */
static char *data_space_start;
#ifdef MSTATS
static int nmalloc[30];
static int nmal, nfre;
#endif /* MSTATS */
/* If range checking is not turned on, all we have is a flag indicating
whether memory is allocated, an index in nextf[], and a size field; to
realloc() memory we copy either size bytes or 1<<(index+3) bytes depending
on whether the former can hold the exact size (given the value of
'index'). If range checking is on, we always need to know how much space
is allocated, so the 'size' field is never used. */
struct mhead {
char mh_alloc; /* ISALLOC or ISFREE */
char mh_index; /* index in nextf[] */
/* Remainder are valid only when block is allocated */
unsigned short mh_size; /* size, if < 0x10000 */
#ifdef rcheck
unsigned mh_nbytes; /* number of bytes allocated */
int mh_magic4; /* should be == MAGIC4 */
#endif /* rcheck */
};
/* Access free-list pointer of a block.
It is stored at block + 4.
This is not a field in the mhead structure
because we want sizeof (struct mhead)
to describe the overhead for when the block is in use,
and we do not want the free-list pointer to count in that. */
#define CHAIN(a) \
(*(struct mhead **) (sizeof (char *) + (char *) (a)))
#ifdef rcheck
/* To implement range checking, we write magic values in at the beginning and
end of each allocated block, and make sure they are undisturbed whenever a
free or a realloc occurs. */
/* Written in each of the 4 bytes following the block's real space */
#define MAGIC1 0x55
/* Written in the 4 bytes before the block's real space */
#define MAGIC4 0x55555555
#define ASSERT(p) if (!(p)) botch("p"); else
#define EXTRA 4 /* 4 bytes extra for MAGIC1s */
#else
#define ASSERT(p) if (!(p)) abort (); else
#define EXTRA 0
#endif /* rcheck */
/* nextf[i] is free list of blocks of size 2**(i + 3) */
static struct mhead *nextf[30];
/* busy[i] is nonzero while allocation of block size i is in progress. */
static char busy[30];
/* Number of bytes of writable memory we can expect to be able to get */
static unsigned int lim_data;
/* Level number of warnings already issued.
0 -- no warnings issued.
1 -- 75% warning already issued.
2 -- 85% warning already issued.
*/
static int warnlevel;
/* Function to call to issue a warning;
0 means don't issue them. */
static void (*warnfunction) ();
/* nonzero once initial bunch of free blocks made */
static int gotpool;
char *_malloc_base;
static void getpool ();
/* Cause reinitialization based on job parameters;
also declare where the end of pure storage is. */
void
malloc_init (start, warnfun)
char *start;
void (*warnfun) ();
{
if (start)
data_space_start = start;
lim_data = 0;
warnlevel = 0;
warnfunction = warnfun;
}
/* Return the maximum size to which MEM can be realloc'd
without actually requiring copying. */
int
malloc_usable_size (mem)
char *mem;
{
struct mhead *p
= (struct mhead *) (mem - ((sizeof (struct mhead) + 7) & ~7));
int blocksize = 8 << p->mh_index;
return blocksize - sizeof (struct mhead) - EXTRA;
}
static void
morecore (nu) /* ask system for more memory */
register int nu; /* size index to get more of */
{
char *sbrk ();
register char *cp;
register int nblks;
register unsigned int siz;
int oldmask;
#ifdef BSD
#ifndef BSD4_1
int newmask = -1;
/* Blocking these signals interferes with debugging, at least on BSD on
the HP 9000/300. */
#ifdef SIGTRAP
newmask &= ~(1 << SIGTRAP);
#endif
#ifdef SIGILL
newmask &= ~(1 << SIGILL);
#endif
#ifdef SIGTSTP
newmask &= ~(1 << SIGTSTP);
#endif
#ifdef SIGSTOP
newmask &= ~(1 << SIGSTOP);
#endif
oldmask = sigsetmask (newmask);
#endif
#endif
if (!data_space_start)
{
data_space_start = start_of_data ();
}
if (lim_data == 0)
get_lim_data ();
/* On initial startup, get two blocks of each size up to 1k bytes */
if (!gotpool)
{ getpool (); getpool (); gotpool = 1; }
/* Find current end of memory and issue warning if getting near max */
#ifndef VMS
/* Maximum virtual memory on VM